package com.badibadi.activity;

import java.util.HashMap;
import java.util.Map;

import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.MySharePreferences;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.id;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
import com.libjph.util.StringUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

public class ToResetYourPasswordActivity extends BaseActivity implements
		OnClickListener {
	private AutoCompleteTextView old_login_edit_username,
			new_login_edit_username, ok_login_edit_username;
	private Button uppic_settings, uppic_back;
	private Results results;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(ToResetYourPasswordActivity.this);
				Utils.showMessage(ToResetYourPasswordActivity.this, getResources().getString(R.string.l_net_error));
				break;
			case 2:
				Utils.ExitPrgress(ToResetYourPasswordActivity.this);
				Utils.showMessage(ToResetYourPasswordActivity.this,
						results.getRetmsg());
				if (results.isRet()) {
					Intent intent = new Intent(
							ToResetYourPasswordActivity.this,
							BeforeLandingPageActivity.class);
					MySharePreferences id = new MySharePreferences(
							ToResetYourPasswordActivity.this, "user");// 获取用户id
					id.Clear();
					startActivity(intent);
					Constants.Dengchu_handler.sendEmptyMessage(3);
					finish();
				}
				break;
			case 3:
				Utils.ExitPrgress(ToResetYourPasswordActivity.this);
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.activity_to_reset_your_password);
		init();
		this.findViewById(R.id.ForgotPassword).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(
								ToResetYourPasswordActivity.this,
								ForgotPassWordActivity.class);
						startActivity(intent);
					}
				});

	}

	private void init() {
		old_login_edit_username = (AutoCompleteTextView) findViewById(R.id.old_login_edit_username);
		new_login_edit_username = (AutoCompleteTextView) findViewById(R.id.new_login_edit_username);
		ok_login_edit_username = (AutoCompleteTextView) findViewById(R.id.ok_login_edit_username);
		uppic_settings = (Button) findViewById(R.id.uppic_settings);
		uppic_back = (Button) findViewById(R.id.uppic_back);
		uppic_back.setOnClickListener(this);
		uppic_settings.setOnClickListener(this);
	}

	private void Set_password(final String old, final String new_) {
		Utils.showPrgress(ToResetYourPasswordActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", Utils.getUid(ToResetYourPasswordActivity.this));
				map.put("oldPass", old);
				map.put("newPass", new_);
				String content = Utils.sendRequest(map, Constants.BadiUrl
						+ "set_password");
				if (content != null) {
					results = Utils.checkResult_NNN(getApplicationContext(),
							content);
					if (results != null && !results.getRetmsg().equals("null")) {
						handler.sendEmptyMessage(2);
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.uppic_settings:
			String new_ = null,
			old_ = null,
			ok_ = null;
			old_ = old_login_edit_username.getText().toString().trim();
			new_ = new_login_edit_username.getText().toString().trim();
			ok_ = ok_login_edit_username.getText().toString().trim();
			if (!StringUtil.isNullOrEmpty(old_)
					&& !StringUtil.isNullOrEmpty(new_)
					&& !StringUtil.isNullOrEmpty(ok_)) {
				if (new_.equals(ok_)) {
					Set_password(old_, new_);
				} else {
					Toast.makeText(getApplicationContext(), getResources().getString(R.string.l_xa3), 0).show();
				}
			} else {
				Toast.makeText(getApplicationContext(), getResources().getString(R.string.l_xb145), 0).show();
			}
			break;
		case R.id.uppic_back:
			this.finish();
			break;

		default:
			break;
		}
	}
}
